<template>
  <div class="goods">
    <div class="wrap">
      <Crumb></Crumb>
      <img src="../assets/img/banner.png" width="100%" alt />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Num }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
          >{{ item.txt }}</span>
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
          >{{ item.txt }}</span>
        </li>
      </ul>
      <List :arr="goodsListShow" />
      <div style="text-align: center;">{{isReachBottom?"已经没有数据了":"正在加载中...."}}</div>
    </div>
  </div>
</template>                            
<script>

import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { goodsSearchApi } from "@/request/api"
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils"
export default {
  components: {
    Crumb,
    List,
  },
  data() {
    return {
      did: 0,//栏目  目前可提供的参数是 1->精品推荐 2-> 热门兑换 0->全部
      type: 0,//  ( 1->实体商品 2->虚拟商品 0->全部 )
      min: 0,//大于多少积分
      max: 0,//小于多少积分 最多是10000，当传入0的时候，会直接返回所有商品，无视后台逻辑。
      keyword: '',//商品搜索关键字
      goodsList: [],//商品列表所有数据
      goodsListShow: [],//商品列表展示数据
      size: 8,
      page: 1,
      isLoading: false,
      isReachBottom:false,//是否还有数据
      options1: [// 排序的数组项
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Num: 0,//排序数组的高亮控制
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
    };
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal
          this.goodsSearch()
        }
      }
    }
  },
  methods: {
    // 排序选择的函数
    options1Fn(index, min, max) {
      this.options1Num = index
      this.max = max
      this.min = min
      this.goodsSearch()
    },
    // 分类选择的函数
    options2Fn(index, type) {
      // this.options2.isActive = false
      this.options2.forEach((item) => {
        item.isActive = false
      })
      this.options2[index].isActive = true
      this.type = type
      this.goodsSearch()
    },
    scrollFn() {
      // console.log(
      // getScrollTop(),
      // getClientHeight(),
      // getScrollHeight()
      // );

      // 当滚动高度+可视化窗口高度的值大于等于文档的高度时加载数据
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 20) {//表示已经滑倒底部了
        if(this.goodsListShow.length>=this.goodsList.length){
          this.isReachBottom = true
          return
        }
        if (this.isLoading == false) {
          this.isLoading = true
          this.page++
          // 因为滑块滚动过快，此处会不断的进行循环
          // 节流   设置一个变量控制，让一段代码在一段时间内只执行一次
          setTimeout(() => {
            for (let i = this.size * (this.page - 1); i < this.size * (this.page); i++) {//遍历当前页的数据 this.goodsList[i]当前页每条数据
              console.log("要往show里面push数据", this.page, this.goodsList[i]);
              this.goodsList[i] && this.goodsListShow.push(this.goodsList[i])
            }
            this.isLoading = false
          }, 3000)
        }


      }
    },
    // 商品搜索的函数封装
    async goodsSearch() {
      let res = await goodsSearchApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      })
      this.goodsList = res.data
      this.goodsListShow = res.data.filter((item, index) => index < 8)

      // 页面没有刷新，可能导致page++
      // 初始化
      this.page = 1
      this.isReachBottom = false
      // 如果没有查询到数据  goodList = 0
      if(this.goodsListShow.length>=this.goodsList.length){
        this.isReachBottom = true
      }
    }
  },
  created() {
    this.keyword = this.$route.query.keyword
    this.goodsSearch()
  },
  mounted() {
    console.log("添加监听");
    window.addEventListener("scroll", this.scrollFn)
  },
  beforeDestroy() {
    console.log("取消监听");
    window.removeEventListener("scroll", this.scrollFn)
  }
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/global.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>